﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace Demo.OData.Views
{
  public partial class Basic : Page
  {
    public Basic()
    {
      InitializeComponent();
    }

    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void RowUp_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.MoveCurrentToPrevious();
    }

    private void RowDown_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.MoveCurrentToNext();
    }

    private void PageDown_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.MoveToNextPage();
    }

    private void PageUp_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.MoveToPreviousPage();
    }

    private void RejectChanges_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.RejectChanges();
    }

    private void RemoveItem_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.Remove(dsProducts.Data.CurrentItem);
    }

    private void SubmitChanges_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.SubmitChanges(null);
     
    }

    private void AddItemA_Click(object sender, RoutedEventArgs e)
    {
      Slma.OData.Entity item = dsProducts.Data.CreateEntity();
      // The Product entity class doesn't inherit from Demo.OData.CustomService.Product but it has got the same property. 
      // Because the entity is built dynamicaly, you have to set property below.
      item.SetValue("ProductID",Guid.NewGuid());
      item.SetValue("StandardCost",10);
      item.SetValue("ListPrice",8.20);
      item.SetValue("Color", "Silver");
      item.SetValue("Size", 44);
      item.SetValue("Weight",0);
      item.SetValue("SellStartDate",DateTime.Now.Date);
      item.SetValue("Name","G Mountain Bike G");
      item.SetValue("ProductNumber","XX-999");

      item.ValidatePropertyValue = true; // Entity validates property values;
      dsProducts.Data.Add(item);
    }
    private void AddItemB_Click(object sender, RoutedEventArgs e)
    {
      Slma.OData.Entity item = dsProducts.Data.CreateEntity();
      item.ValidatePropertyValue = true; // Entity validates property values;

      // The Product entity class doesn't inherit from Demo.OData.CustomService.Product but it has got the same property. 
      // Because the entity is built dynamicaly, you have to set property below.
      item.SetValuesFromODataEntity(new Demo.OData.CustomService.Product() {
        ProductID = Guid.NewGuid(),
        Name ="B Mountain Bike B",
        ProductNumber = "XX-888",
        Color ="Green",
        ListPrice = 27.33M,
        StandardCost = 30M,
        Weight = 0,
        Size =35,
        SellStartDate = DateTime.Now.Date
      });

      dsProducts.Data.Add(item);
    }

    private void UpdateItem_Click(object sender, RoutedEventArgs e)
    {
      var item = dsProducts.Data.CurrentItem as Slma.OData.Entity;
      item.SetValue("Color", "Multi");
    }

    private void RefreshData_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.Data.Refresh();
    }

    private void Sort_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.SortDescriptions.Clear();
      dsProducts.SortDescriptions.Add(new SortDescriptionOData() { PropertyName = "Size", Direction = SortDescriptionDirectionOData.Descending });
      dsProducts.SortDescriptions.Add(new SortDescriptionOData() { PropertyName = "ListPrice" });
      dsProducts.ApplySortDescriptions();
    }

    private void Filter01_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.FilterDescriptions = new FilterDescriptorLogicalOData() {
        Operator = FilterDescriptorLogicalOperator.And,
        Left = new FilterDescriptorExpressionOData() { PropertyName = "Name", Operator = FilterDescriptorExpressionOperator.Contains, Value = "mountain" },
        Right = new FilterDescriptorLogicalOData() {
          Operator = FilterDescriptorLogicalOperator.Or,
          Left = new FilterDescriptorExpressionOData() { PropertyName="Color",Operator=FilterDescriptorExpressionOperator.Contains,Value="Silver"},
          Right = new FilterDescriptorExpressionOData() { PropertyName="Color",Operator=FilterDescriptorExpressionOperator.Contains,Value="White"}
        }
      };
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      dsProducts.FilterDescriptions = null;
    }

    private void WaitControl_Cancel(object sender, RoutedEventArgs e)
    {
      if(dsProducts.IsLoading) dsProducts.CancelLoading();
      else if(dsProducts.IsSubmitting) dsProducts.CancelSubmitting();
    }


    private void dsProducts_AddedNew(object sender, Slma.OData.EventEntityAddingNewEntityArgs e)
    {
      e.NewEntity.SetValue("ProductID", Guid.NewGuid());
      e.NewEntity.SetValue("SellStartDate",DateTime.Now.Date);
    }

    private void DGRB_CreateDataForm(object sender, DataFormEventArgs e)
    {
      e.DataForm = new ProductFormODATA();
    }

  }
}
